<template>
	<el-container v-loading="loading">
		<el-header>
			<div class="left-panel">
				<ex-select @change="changeSelect" placeholder="请选择呆滞期" v-model="stagnantDay" dict-code="ASSET_STAGNANT_PERIOD" clearable="true">
				</ex-select>
				<fsButton style="margin-left: 5px;" ref="button" plain size="small" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
			<div class="right-panel">
				<ex-search ref="search" :searches="searches" @searchOk="searchHandler"
						   default-code="code" placeholder="请输入资产编码"></ex-search>
			</div>
		</el-header>
		<el-main class="nopadding" style="padding: 0 5px;">
			<ex-table ref="table" :selection="false" :columns="columns" :auto-refresh="false"
					  :url="this.$API.asset.asset.assetStagnantPeriodQuery.url"
					  :refs="this.$refs" search="search" @columnClick="columnClickHandler">

			</ex-table>
		</el-main>
	</el-container>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import scSelectFilter from "@/components/scSelectFilter/index.vue";
import tool from "@/utils/tool";
import moment from "moment";
import ExSelect from "@/components/exSelect/index.vue";
import globalparam from "@/constant/model/globalparam";
export default {
	name: '呆滞资产统计',
	components: {
		ExSelect,
		scSelectFilter,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
	},
	data() {
		return {
			dialog: {
				save: false,
				permission: false,
			},
			apiObj: [],
			buttons: [
				{name: "导出", icon: "el-icon-download", click: "export",  type: "info", permission: "export"},
			],
			searches: [
				{type: "daterange", name: "购置日期", code: "boughtDateStart,boughtDateEnd", placeholder: "请选择购置日期", formatter:"YYYY-MM-dd",valueFormat:"YYYYMMDD",range:30},
				{type: "input", name: "资产编码", code: "code", placeholder: "请输入资产编码", maxlength: 30, showLimit: true},
				{type: "input", name: "资产名称", code: "name", placeholder: "请输入资产名称"},
				{type: "tree-select", name: "资产分类", code: "assetCategoryIds", placeholder: "请选择资产分类", dictCode:'FamsAssetCategory',checkStrictly:true,multiple:true,dataPermissionProcess:true,permissionData:this.$TOOL.current.manageAssetCategoryIds()},
				{type: "tree-select", name: "使用部门", code: "usingDepartmentIds", placeholder: "请选择使用部门", dictCode:'SysOrganization',checkStrictly:true,multiple:true,dataPermissionProcess:true,permissionData:this.$TOOL.current.readDeptIds()},
				{type: "selector", name: "使用人", code: "usingMenId", placeholder: "请选择使用人", dictCode:'SysUser'},
				{type: "select", name: "资产状态", code: "assetStatusIds", placeholder: "请选择资产状态", dictCode:'ASSET_STATUS',multiple:true},
				{type: "tree-select", name: "资产位置", code: "storePlaceId", placeholder: "请选择资产位置", dictCode:'FamsPosition'},
			],
			columns: [
				{code: "assetStatusId", name: "资产状态", width:"85",formatter:"DICT",key: "ASSET_STATUS", sortable: true},
				{code: "code", name: "资产编码", width:"135"},
				{code: "financialCode", name: "财务编码", width:"170"},
				{code: "name", name: "资产名称", width:"200"},
				{code: "stagnantDay", name: "闲置天数", width:"90", sortable: true},
				{code: "boughtDate", name: "购置日期", width:"110",formatter: "DATE", sortable: true},
				{code: "assetCategoryId", name: "资产分类",width:"220", formatter:"DICT",key: "FamsAssetCategory", sortable: true},
				{code: "storePlaceId", name: "资产位置",width: "220", formatter:"DICT",key: "FamsPosition", sortable: true},
				{code: "brand", name: "品牌", width:""},
				{code: "model", name: "规格型号", width:""},
				{code: "serialCode", name: "资产序列号", width:"150"},
				{code: "assetCount", name: "数量", width:"50"},
				{code: "originalValue", name: "资产原值", width:"150"},
				{code: "netValue", name: "资产净值", width:"150"},
				{code: "usingMenId", name: "使用人", width:"110",formatter: "DICT",key: "SysUser"},
				{code: "usingDepartmentId", name: "使用部门", width:"220",formatter: "DICT",key: "SysOrganization"},
			],
			parentLevelCode:"",
			assetIds:[],
			loading: false,
			stagnantDay:'',
			readDeptIds: this.$TOOL.current.readDeptIds(),
			// manageDeptIds: this.$TOOL.current.manageDeptIds(),
			manageAssetCategoryIds: this.$TOOL.current.manageAssetCategoryIds(),
			loginUsers: tool.current.user(),
		}
	},
	created() {
	},
	mounted() {
	},
	beforeMount() {
		this.$cache.data.loadGlobalParam(globalparam.assetStatisticsRange.paramCode).then((res) => {
			this.assetStatisticsRange = res;
			this.searchHandler();
		});
	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		// async searchTreeHandler(data) {
		// 	let queryParams = {pathCode:data.levelCode}
		// 	this.parentLevelCode= data.levelCode;
		// 	await this.$refs.table.refresh(queryParams);
		// },
		changeSelect(){
			this.searchHandler();
		},
		async searchHandler() {
			let queryParams = {};
			if(this.stagnantDay){
				queryParams.stagnantDay = this.stagnantDay;
			}
			this.fomateQueryParams(queryParams,this);
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name,data){
			this.$options.methods[name](this, data);
		},
		//导出
		async export(_this,index) {
			_this.$refs.button.loading(index, true);
			let queryPrams = {};
			if(_this.stagnantDay){
				queryPrams.stagnantDay = _this.stagnantDay;
			}
			_this.fomateQueryParams(queryPrams,_this);
			let selections = _this.$refs.table.selections();
			if (selections.length > 0) {
				queryPrams.assetCodes = selections.map(item => item.code);
			}
			queryPrams.exportType = "stagnantAsset";
			let res = await _this.$API.asset.asset.export.post(queryPrams);
			if (res.code === 2000) {
				await _this.$alert(_this.$t('common.export_request_success'), "提示", {type: 'success'}).catch(()=>{});
			}
			_this.$refs.button.loading(index, false);
		},
		fomateQueryParams(queryParams,_this){
			Object.assign(queryParams, _this.$refs.search.searchParams());
			if(!queryParams.usingDepartmentIds||queryParams.usingDepartmentIds.length===0){
				queryParams.usingDepartmentIds = _this.readDeptIds;
			}
			if(!queryParams.assetCategoryIds||queryParams.assetCategoryIds.length===0){
				queryParams.assetCategoryIds = _this.manageAssetCategoryIds;
			}
		},
	}
}

</script>
<style scoped>

</style>
